home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / LPCView / Source / Help.m < prev    next >
Encoding:
Text File  |  1992-03-07  |  5.3 KB  |  220 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Help.h"
  5. #import "Dispatcher.h"
  6. #import <appkit/Button.h>
  7. #import <appkit/Cell.h>
  8. #import <appkit/Matrix.h>
  9. #import <appkit/MenuCell.h>
  10. #import <appkit/NXBrowser.h>
  11. #import <appkit/NXBrowserCell.h>
  12. #import <appkit/ScrollView.h>
  13. #import <appkit/Text.h>
  14. #import <dpsclient/wraps.h>
  15. #import <sys/dir.h> //for getdirentries()
  16. #import <libc.h>     
  17. #import <strings.h>
  18.  
  19. @implementation Help
  20.  
  21. - init
  22. {
  23.     if (!strlen([NXApp appDirectory]))
  24.         [NXApp init];
  25.     sprintf(helpDir, "%s/%s", [NXApp appDirectory], "Help");
  26.     printf("helpDir = %s\n", helpDir);
  27.     sprintf(noHelpFile, "%s/%s", helpDir, "No Help.rtf");
  28.     helpPanel = [NXApp loadNibSection:"help.nib" owner:self];
  29.     return self;
  30. }
  31.  
  32. - setHelpBrowser:anObject
  33. {
  34.     helpBrowser = anObject;
  35.     [helpBrowser setDelegate:self];
  36.     [helpBrowser loadColumnZero];
  37.     return self;
  38. }
  39.  
  40. - generalHelp:sender
  41. {
  42.     [self showHelpFile:"Overview"];
  43.     return self;
  44. }
  45.  
  46. - browserHit:sender
  47. {
  48.     [self showHelpFile:[[[sender matrixInColumn:0] selectedCell] stringValue]];
  49.     return self;
  50. }
  51.  
  52. - print:sender
  53. {
  54.     [[helpScroll docView] printPSCode:sender];
  55.     return self;
  56. }
  57.  
  58. - showHelpFile:(const char *)filename
  59. {
  60.    NXStream *stream;
  61.    char helpFile[MAXPATHLEN];
  62.    static NXPoint origin = {0.0,0.0};
  63.    
  64.  
  65. if (![self browser:helpBrowser selectCell:filename inColumn:0])
  66.         [self browser:helpBrowser selectCell:"No Help" inColumn:0]; 
  67.     sprintf(helpFile,"%s/%s.rtf",helpDir,filename); 
  68.     if ((stream = NXMapFile(helpFile,NX_READONLY)) == NULL)
  69.         stream = NXMapFile(noHelpFile,NX_READONLY);
  70.     if (stream != NULL) {
  71.         [helpPanel disableFlushWindow];
  72.         [[helpScroll docView] readRichText:stream]; 
  73.     [[helpScroll docView] scrollPoint:&origin];
  74.     [[helpPanel reenableFlushWindow] flushWindow];
  75.         NXCloseMemory(stream,NX_FREEBUFFER);
  76.     }
  77.     [helpPanel orderFront:self];
  78.     return self;
  79. }
  80.  
  81. #define CHUNK    127
  82. static char **addFile(const char *file, int length, char **list, int count)
  83. {
  84.     char    *suffix;
  85.     
  86.     if (!list) list = (char**)malloc(CHUNK*sizeof(char *));
  87.     if (suffix = rindex(file,'.'))
  88.         *suffix = '\0';
  89.     list[count] = (char *)malloc((length + 1)*sizeof(char));
  90.     strcpy(list[count], file);
  91.     count++;
  92.     if (!(count % CHUNK)) {
  93.         list = (char **)realloc(list, (((count/CHUNK)+1)*CHUNK)*sizeof(char *));
  94.     }
  95.     list[count] = NULL;
  96.     return list;
  97. }
  98.  
  99. static void freeList(char **list)
  100. {
  101.     char    **strings;
  102.     
  103.     if (list) {
  104.         strings = list;
  105.         while (*strings)
  106.             free(*strings++);
  107.         free(list);
  108.     }
  109. }
  110.  
  111. static BOOL isOk(const char *s)
  112. /* checks to make sure the filename is not NULL and to verify that it is
  113.  * not a "dot"--hidden file.
  114.  */
  115. {
  116.     return (!s[0] || s[0] == '.') ? NO : YES;
  117. }
  118.  
  119. static int caseInsensitiveCompare(void *arg1, void *arg2)
  120. /* Compares the two arguments without regard for case using strcasecmp().
  121. */
  122. {
  123.     char *string1, *string2;
  124.  
  125.     string1 = *((char **)arg1);
  126.     string2 = *((char **)arg2);
  127.     return strcasecmp(string1,string2);
  128. }
  129.  
  130. static char **fileList;
  131.  
  132. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  133. /* This delegate method goes out to the help directory and gets a list
  134.  * of all the files in that directory.  It creates a list of file names
  135.  * for the static variable fileList, and will load the filenames into the 
  136.  * browser on demand (lazy loading).
  137.  */
  138. {
  139.     long basep;
  140.     char *buf;
  141.     struct direct *dp;
  142.     char **list = NULL;
  143.     int cc, fd, fileCount = 0;
  144.     char dirbuf[8192];
  145.  
  146.     if ((fd = open(helpDir, O_RDONLY, 0644)) > 0) {
  147.     cc = getdirentries(fd, (buf = dirbuf), 8192, &basep);
  148.     while (cc) {
  149.         dp = (struct direct *)buf;
  150.         if (isOk(dp->d_name)) {
  151.         list = addFile(dp->d_name, dp->d_namlen, list, fileCount++);
  152.         }
  153.         buf += dp->d_reclen;
  154.         if (buf >= dirbuf + cc) {
  155.         cc = getdirentries(fd, (buf = dirbuf), 8192, &basep);
  156.         }
  157.     }
  158.     close(fd);
  159.     if (list) qsort(list,fileCount,sizeof(char *),caseInsensitiveCompare);
  160.     }
  161.     freeList(fileList);
  162.     fileList = list;
  163.     return fileCount;
  164. }
  165.  
  166. - browser:sender loadCell:cell atRow:(int)row inColumn:(int)column
  167. /* This delegate method loads the cell for a given row.  The stringValue
  168.  * for that row comes from the fileList.
  169.  */
  170. {
  171.     if (fileList) {
  172.     [cell setStringValueNoCopy:fileList[row]];
  173.     [cell setLeaf:YES];
  174.     }
  175.     return self;
  176. }
  177.  
  178.  
  179. - (BOOL)browser:sender selectCell:(const char *)title inColumn:(int)column
  180. /* This delegate method selects the cell with the given title.  If it finds
  181.  * a cell with that title, it verifies that it has a file entry in the 
  182.  * fileList, forces the loading of the cell, selects it (highlights) and
  183.  * scrolls the browser so the cell is visible.  It returns a boolean value
  184.  * which indicates whether the cell was found.
  185.  */
  186. {
  187.     int row;
  188.     id matrix;
  189.  
  190.     if (title) {
  191.     matrix = [sender matrixInColumn:column];
  192.     if (!fileList) return NO;
  193.     for (row = [matrix cellCount]-1; row >= 0; row--) {
  194.         if (fileList[row] && !strcmp(title, fileList[row])) {
  195.         [sender getLoadedCellAtRow:row inColumn:column];
  196.         [matrix selectCellAt:row :0];
  197.         [matrix scrollCellToVisible:row :0];
  198.         return YES;
  199.         }
  200.     }
  201.     }
  202.     return NO;
  203. }
  204.  
  205.  
  206. /* WINDOW DELEGATE METHODS */
  207.  
  208. - windowWillResize:sender toSize:(NXSize *)frameSize;
  209. /* This method constrains the Help Panel to a reasonable minimum size
  210.  * when the user resizes the panel.
  211.  */
  212. {
  213.     frameSize->width = MAX(frameSize->width,400.0);
  214.     frameSize->height = MAX(frameSize->height,350.0);
  215.     return self;
  216. }
  217.  
  218.  
  219. @end
  220.